from gis.common import track_logging
from django.views.decorators.http import require_POST

from gis.common.django_ext.decorators import validate_parameters
from itpay.core.agency.const import AgencyScoreEventType, AgencySortScoreType
from itpay.core.recharge_order import recharge_comment_service
from . import form
from itpay.core.agency import agency_score_service, agency_sort_service

_LOGGER = track_logging.getLogger(__name__)


@require_POST
@validate_parameters(form.AddRechargeCommentListSchema)
def recharge_comment(request, cleaned_data):
    order_id = cleaned_data['recharge_order_id']
    comment_star = cleaned_data['comment_star']
    comment_content = cleaned_data['comment_content']
    comment = recharge_comment_service.add_pay_order_comment(order_id, comment_star, comment_content)

    try:
        agency_score_service.update_agency_score(comment['agency_id'], AgencyScoreEventType.ORDER_COMMENT,
                                                 comment['id'], comment['mch_id'], comment['user_id'])

        agency_sort_service.update_agency_score(comment['agency_id'], AgencySortScoreType.USER_COMMENT,
                                                comment['id'])

    except Exception as e:
        _LOGGER.exception("add recharge_comment score exception {}".format(e))

    return comment
